<?php
include("./config.php");
include("./utils.php");


$op = $_GET['op'];


function get_sql($appid, $op, $id){
  $sql = "";
  switch($op){
  # -------------------------------------------------------
  case 'one':
    $id = safe(getGET('id'));
    $sql = "SELECT `id`, `parentid`, `name`, `sex`,"
      ." `generation`, `note`, `addr`, `wife_addr`, `phone`, `mobile1`,`mobile2`,`mobile3`,"
      ."`born_time`,`die_time`,`wife_born_time`,`wife_die_time` FROM `familya_item` WHERE `id`=$id";
    break;
  case 'next-one':
    $generation = safe(getGET('generation'));
    $id = safe(getGET('id'));
    $sql = "SELECT `id`, `parentid`, `name`, `sex`,"
      ." `generation`, `note`, `addr`, `wife_addr`, `phone`, `mobile1`,`mobile2`,`mobile3`,"
      ."`born_time`,`die_time`,`wife_born_time`,`wife_die_time` FROM `familya_item` WHERE `id`!=$id AND `generation`>=$generation ORDER BY `generation` DESC LIMIT 1";
    break;
  case 'prev-one':
    $generation = safe(getGET('generation'));
    $id = safe(getGET('id'));
    $sql = "SELECT `id`, `parentid`, `name`, `sex`,"
      ." `generation`, `note`, `addr`, `wife_addr`, `phone`, `mobile1`,`mobile2`,`mobile3`,"
      ."`born_time`,`die_time`,`wife_born_time`,`wife_die_time` FROM `familya_item` WHERE `id`!=$id AND `generation`<=$generation ORDER BY `generation` ASC LIMIT 1";
    break;
  case 'one-count':
    $id = safe(getGET('id'));
    $sql = "SELECT parentid as id, count(parentid) as `count` from familya_item where parentid=$id";
    break;
  case 'list':
    $start = safe(getGET('start'));
    $count = safe(getGET('count'));
    if ($count > 100) $count = 100;
    $sql = "SELECT `id`, `parentid`, `name`, `sex`,"
      ." `generation`, `note`, `addr`, `wife_addr`, `phone`, `mobile1`,`mobile2`,`mobile3`,"
      ."`born_time`,`die_time`,`wife_born_time`,`wife_die_time` FROM `familya_item` ORDER BY `generation` ASC LIMIT $start, $count";
    break;
  case 'list-count':
    $start = safe(getGET('start'));
    $count = safe(getGET('count'));
    $sql = "SELECT parentid as id, count(parentid) as `count` from familya_item where parentid in "
      ." (select t.id from (SELECT `id` FROM `familya_item` ORDER BY `generation` ASC LIMIT $start, $count) as t) group by parentid";
    break;
  case 'sons':
    $id = safe(getGET('id'));
    $sql = "SELECT `id`, `parentid`, `name`, `sex`,"
      ." `generation`, `note`, `addr`, `wife_addr`, `phone`, `mobile1`,`mobile2`,`mobile3`,"
      ."`born_time`,`die_time`,`wife_born_time`,`wife_die_time` FROM `familya_item` WHERE `parentid`=$id";
    break;
  case 'sons-count':
    $id = safe(getGET('id'));
    $sql = "SELECT parentid as id, count(parentid) as `count` from familya_item where parentid in "
      ." (SELECT `id` FROM `familya_item` WHERE `parentid`=$id) group by parentid";
    break;
  case 'sons-with-depth':
    $id = safe(getGET('id'));
    $depth = intval(safe(getGET('depth')));
    if ($depth <= 0) $depth = 1;
    if ($depth > 6) $depth = 6;
    $sql = "SELECT `id`, `parentid`, `name`, `sex`,"
      ." `generation`, `note`, `addr`, `wife_addr`, `phone`, `mobile1`,`mobile2`,`mobile3`,"
      ." `born_time`,`die_time`,`wife_born_time`,`wife_die_time` FROM `familya_item` "
      ." WHERE id in ";

    $base_sql = "$id";
    for($i = 0; $i < $depth; $i=$i+1){
      $base_sql = "(SELECT DISTINCT `id` from familya_item where parentid in ($base_sql) OR id in ($base_sql))";
    }

    $sql = $sql.$base_sql;
    break;
  case 'sons-with-depth-count':
    $id = safe(getGET('id'));
    $depth = intval(safe(getGET('depth')));
    if ($depth <= 0) $depth = 1;
    if ($depth > 6) $depth = 6;

    $base_sql = "($id)";
    for($i = 0; $i < $depth; $i=$i+1){
      $base_sql = "(SELECT DISTINCT `id` from familya_item where parentid in ($base_sql) OR id in ($base_sql))";
    }

    $sql = "SELECT parentid as id, count(parentid) as `count` from familya_item where parentid in "
      ." $base_sql group by parentid";
    break;

  # -------------------------------------------------------
  case 'by-name':
    $name = safe(getGET('name'));
    $sql = "SELECT `id`, `parentid`, `name`, `sex`,"
      ." `generation`, `note`, `addr`, `wife_addr`, `phone`, `mobile1`,`mobile2`,`mobile3`,"
      ."`born_time`,`die_time`,`wife_born_time`,`wife_die_time` FROM `familya_item` WHERE `name`='$name' LIMIT 200";
    break;
  case 'by-generation':
    $generation = safe(getGET('generation'));
    $sql = "SELECT `id`, `parentid`, `name`, `sex`,"
      ." `generation`, `note`, `addr`, `wife_addr`, `phone`, `mobile1`,`mobile2`,`mobile3`,"
      ."`born_time`,`die_time`,`wife_born_time`,`wife_die_time` FROM `familya_item` WHERE `generation`=$generation LIMIT 200";
    break;
  # -------------------------------------------------------
    


  default:
    break;
  }
  return $sql;
}


if ($op == "family") {
  $result = array();
  $ops = array("one","one-count", "sons-with-depth","sons-with-depth-count");
  foreach ($ops as $x) {
    $sql = get_sql($appid, $x, $id);
    $tmp = get_result($sql);
    $result[$x] = $tmp;
  }
} else if($op == "home-list") {
  $result = array();
  $ops = array("list", "list-count");
  foreach ($ops as $x) {
    $sql = get_sql($appid, $x, $id);
    $tmp = get_result($sql);
    $result[$x] = $tmp;
  }
} else if($op == "sons-list") {
  $result = array();
  $ops = array("sons", "sons-count");
  foreach ($ops as $x) {
    $sql = get_sql($appid, $x, $id);
    $tmp = get_result($sql);
    $result[$x] = $tmp;
  }
} else {
  $sql = get_sql($appid, $op, $id);
  if($sql != ""){
    $result = get_result($sql);
  }
}

if (empty($result)) {
  echo "{code:0x1002, msg:'no data'}";
} else {
  echo json_encode($result);
}

?>

